#include "CP.hpp"
#include <unistd.h>
#include "Task.hpp"
#include <time.h>
#include <stdlib.h>

string soper = "+-*/%";

void *productor(void *args)
{
    BlockQueue<Task> *ptr = static_cast<BlockQueue<Task> *>(args);

    while (true)
    {
        int data1 = rand()%11 + 1;
        int data2 = rand()%11 + 1;
        int op = rand()%5;

        Task task(data1,data2,soper[op]);

        usleep(1000);
        ptr->push(task);
        cout << "生产了一个数据 : " << task.GetQuestion() << endl;
    }
}

void *consumer(void *args)
{
    BlockQueue<Task> *ptr = static_cast<BlockQueue<Task> *>(args);

    while (true)
    {
        sleep(2);
        Task data = ptr->pop();
        data.run();
        cout << "消费了一个数据 : " << data.GetResult() << endl;
    }
}

int main()
{
    srand(time(nullptr));

    BlockQueue<Task> *ptr = new BlockQueue<Task>;

    pthread_t tid1;
    pthread_create(&tid1, nullptr, productor, ptr);

    pthread_t tid2;
    pthread_create(&tid2, nullptr, consumer, ptr);

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);

    return 0;
}